var app = getApp()
const request = require('../../utils/request.js')
const util = require('../../utils/util.js')
Page({
  data: {
    /** 
     * 页面配置 
     */
    winWidth: 0,
    winHeight: 0,
    order_list: [],
    tab_list: [
      "全部订单",
      "待付款",
      "待发货",
      "待收货",
      "待评价",
    ],
    isOrderList: true,
    iconlists: [{
        name: "全部订单图标",
        normal: "../../images/ps_page/all.png",
        focus: "../../images/ps_page/all_selected.png"
      },
      {
        name: "待付款图标",
        normal: "../../images/ps_page/payment.png",
        focus: "../../images/ps_page/payment_selected.png"
      },
      {
        name: "待发货图标",
        normal: "../../images/ps_page/send.png",
        focus: "../../images/ps_page/send_selected.png"
      },
      {
        name: "待收货图标",
        normal: "../../images/ps_page/delivered.png",
        focus: "../../images/ps_page/delivered_selected.png"
      },
      {
        name: "待评价图标",
        normal: "../../images/ps_page/comment.png",
        focus: "../../images/ps_page/comment_selected.png"
      },

    ],
    currentTab: 0
  },
  onLoad: function(options) {
    var that = this;
    /*
      order status:
      0 -> 已完成
      1 -> 待付款
      2 -> 待发货
      3 -> 待收货
      4 -> 待评价
      5 -> 交易关闭
      6 -> 交易完成
    */
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
  },
  onHide: function() {
    var that = this;
    that.setData({
      currentTab: 0,
    });
  },
  onShow: function() {
    /**
     * -1: 全部订单
        1 -> 待付款
        2 -> 待发货
        3 -> 待收货
        4 -> 待评价
     */
    var that = this;
    var tab = app.globalData.tab_id;
    console.log('load tab: ' + tab);
    if (tab != null) {
      that.setData({
        currentTab: tab
      });
      app.globalData.tab_id = null;
    }
    var that = this;
    var currentTab = that.data.currentTab;
    if (currentTab == 0) {
      console.log('access from all orders btn');
      that.requestOrders(currentTab);
    }
  },
  /**请求订单 */
  requestOrders: function(orderStatus) {
    var that = this;
    var url = request.URL + '/order';
    console.log('request status: ' + orderStatus);
    if (orderStatus == 0) {
      orderStatus = -1;
    }
    var userId = 1;
    request.request({
      url: url,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: userId,
        orderStatus: orderStatus
      },
      success: function(res) {
        var order_list = res.data;
        for (var i = 0; i < order_list.length; i++) {
          var totalNum = 0;
          var goodArr = order_list[i].goodVOS;
          for (var j = 0; j < goodArr.length; j++) {
            var goodImgArr = goodArr[j].goodImgArray;
            var goodImgUrl = util.string2Array(goodImgArr)[0];
            var goodCnt = goodArr[j].goodCnt;
            totalNum += goodCnt;
            order_list[i].goodVOS[j].goodImgUrl = goodImgUrl;
          }
          order_list[i].totalNum = totalNum;
        }
        that.setData({
          order_list: order_list
        })
        console.log(that.data.order_list);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /** 
   * 滑动切换tab 
   */
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    })
    var currentTab = that.data.currentTab;
    that.requestOrders(currentTab);
  },
  /** 
   * 点击切换tab 
   */
  swichNav: function(e) {
    console.log(e.currentTarget.dataset.current)
    var that = this;
    if (this.data.currentTab === e.currentTarget.dataset.current) {
      //点击的是同一个，则不操作  
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  toOrderDetails: function() {
    wx.navigateTo({
      url: '../order_list/order_details/order_details',
    })
  },
  /**点击删除订单 */
  deleteOrder: function(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    console.log(orderId)
    wx.showModal({
      title: '删除订单',
      content: '删除的订单将无法恢复',
      success: function(res) {
        console.log(res)
        if (res.confirm) {
          var url = request.URL + '/order/delete';
          request.request({
            url: url,
            data: {
              orderId: orderId
            },
            success: function(res) {
              that.onShow();
            },
            fail: function() {
              request.showFailInfo()
            }
          })
        }
      }
    })
  },
  /**进入订单详情 */
  toOrderDetails: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var orderId = that.data.order_list[index].order.orderId;
    console.log(orderId)
    wx.navigateTo({
      url: '/pages/order_list/order_details/order_details?orderId=' + orderId
    })
  },
  /**进入店铺 */
  toStore: function(e) {
    var storeId = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: '/pages/details/shop_details/shop_details?storeId=' + storeId,
    })
  },
  /**点击付款按钮 */
  payForOrder: function(e) {
    /**模拟付款 */
    wx.showModal({
      title: '模拟付款',
      content: '点击确定：付款成功，点击取消：取消付款',
      confirmColor: '#7B68EE',
      success: function(res) {
        console.log(res);
        if (res.confirm) {
          var orderId = e.currentTarget.dataset.orderid;
          console.log(orderId)
          var that = this;
          var url = request.URL + '/order/pay';
          request.request({
            url: url,
            data: {
              orderId: orderId
            },
            success: function(res) {
              wx.navigateTo({
                url: '/pages/order_list/order_details/order_details?orderId=' + orderId,
              })
            },
            fail: function() {
              request.showFailInfo()
            }
          })
        } else {}
      }
    })
  },
  /**点击取消订单按钮 */
  cancelOrder: function(e) {
    wx.showModal({
      title: '取消订单',
      content: '点击确定取消订单',
      confirmColor: '#7B68EE',
      success: function(res) {
        console.log(res);
        if (res.confirm) {
          var orderId = e.currentTarget.dataset.orderid;
          var that = this;
          var url = request.URL + '/order/close';
          request.request({
            url: url,
            data: {
              orderId: orderId
            },
            success: function(res) {
              wx.navigateTo({
                url: '/pages/order_list/order_details/order_details?orderId=' + orderId,
              })
            },
            fail: function() {
              request.showFailInfo()
            }
          })
        } else {}
      }
    })
  },
  /**模拟发货 */
  simPayment: function(e) {
    var orderId = e.currentTarget.dataset.orderid;
    var that = this;
    var url = request.URL + '/order/sendGood';
    request.request({
      url: url,
      data: {
        orderId: orderId
      },
      success: function(res) {
        wx.navigateTo({
          url: '/pages/order_list/order_details/order_details?orderId=' + orderId,
        })
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**确认收货 */
  confirmPayment: function(e) {
    var orderId = e.currentTarget.dataset.orderid;
    var that = this;
    var url = request.URL + '/order/signFor';
    request.request({
      url: url,
      data: {
        orderId: orderId
      },
      success: function(res) {
        wx.navigateTo({
          url: '/pages/order_list/order_details/order_details?orderId=' + orderId,
        })
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**评价商品 */
  toCommentPage: function(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/write_comments/write_comments?orderId=' + orderId
    })
  }
})